const express = require("express");
const app = express();

app.use( express.urlencoded() );
app.use( express.static("./www") );

// [{username:"admin",password:123456},...}]
const userMsg = [];

app.get("/login", (req,res,next)=>{
    const {username, password} = req.query;
    let i=0;
    const flag = userMsg.some((val,idx)=>{
        i = idx;
        return val.username === username;
    })
    if(flag){
        if(userMsg[i].password === password){
            res.send("登录成功");
        }else{
            res.send("密码不对");
        }
    }else{
        res.send("用户名不存在");
    }
})

app.post("/register", (req, res, next)=>{
    // http://localhost:3000/register
    // post方式携带数据

    const {username, password} = req.body;

    const flag = userMsg.some(val=>{
        return val.username === username;
    })

    if(flag){
        console.log(userMsg)
        res.send("注册失败，用户名重复");
    }else{
        userMsg.push({
            username, password
        })
        console.log(userMsg)
        res.send("注册成功");
    }
    
})

app.listen(3000);